package com.leyou.item.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.entity.Sku;
import com.leyou.item.entity.Spu;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import sun.util.resources.ga.LocaleNames_ga;

import java.util.List;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    private final SpuService spuService;

    private final SpuDetailService detailService;

    private final SkuService skuService;

    public GoodsController(SpuService SpuService, SpuDetailService detailService, SkuService skuService) {
        this.spuService = SpuService;
        this.detailService = detailService;
        this.skuService = skuService;
    }


    /**
     * 分页查询spu
     *
     * @param page       当前页
     * @param rows       每页大小
     * @param saleable   上架商品或下降商品
     * @param brandId    品牌id
     * @param categoryId 分类id
     * @param id         spu的id
     * @return 当前页商品数据
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> querySpuByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "saleable", required = false) Boolean saleable,
            @RequestParam(value = "categoryId", required = false) Long categoryId,
            @RequestParam(value = "brandId", required = false) Long brandId,
            @RequestParam(value = "id", required = false) Long id) {

        return ResponseEntity.ok(spuService.querySpuByPage(page, rows, saleable, categoryId, brandId, id));
    }


    /**
     * 新增商品
     *
     * @param spuDTO 页面提交商品信息
     * @return 无
     */
    @PostMapping("/spu")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuDTO spuDTO) {
        spuService.saveGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    /**
     * 修改商品上下架
     *
     * @param id       商品spu的id
     * @param saleable true：上架；false：下架
     * @return 无
     */
    @PutMapping("saleable")
    public ResponseEntity<Void> updateSpuSaleable(
            @RequestParam("id") Long id,
            @RequestParam("saleable") Boolean saleable) {
        spuService.updateSaleable(id, saleable);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    /**
     * 根据id查询商品
     *
     * @param id 商品id
     * @return 商品信息
     */
    @GetMapping("/{id}")
    public ResponseEntity<SpuDTO> queryGoodsById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(spuService.queryGoodsById(id));
    }


    /**
     * 更新商品
     *
     * @param spuDTO 页面提交商品信息
     * @return 无
     */
    @PutMapping("spu")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO) {
        spuService.updateGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    /**
     * 根据spuId查询spuDetail
     *
     * @param id 商品id
     * @return 商品的Detail
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetailById(@RequestParam("id") Long id) {
        return ResponseEntity.ok(new SpuDetailDTO(detailService.getById(id)));
    }


    /**
     * 根据spuID查询对应的sku集合
     *
     * @param id spuID
     * @return sku的集合
     */
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuBySpuId(@RequestParam("id") Long id) {
        return ResponseEntity.ok(
                SkuDTO.convertEntityList(
                        skuService.query().eq("spu_id", id).list()
                )
        );
    }


    /**
     * 根据sku id的集合批量查询sku的集合
     *
     * @param ids id的集合
     * @return sku的集合
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> querySkuBySpuId(@RequestParam("ids") List<Long> ids) {

        return ResponseEntity.ok(this.skuService.querySkuBySpuId(ids));
    }


    /**
     * 根据spuId查询spu的所有规格参数值
     *
     * @param id spu的id
     * @return 规格参数值
     */
    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> querySpecsValues(
            @RequestParam("id") Long id,
            @RequestParam(value = "searching", required = false) Boolean searching) {
        return ResponseEntity.ok(detailService.querySpecValues(id, searching));
    }


    /**
     * 根据id查询商品信息（只查询了spu表）
     *
     * @param id 商品id
     * @return 商品信息
     */
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(new SpuDTO(spuService.getById(id)));
    }

    /**
     * 根据spuId查询商品信息，包括spu和spu详情以及sku信息
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public ResponseEntity<Boolean> deleteSpuById(@PathVariable("id") Long id) {


        return ResponseEntity.ok(spuService.deleteGoodsById(id));
    }


}